<?php

	/**
    * @framework : My Framework
    *
    * @author: fatangel26@gmail.com - Nguyễn Bảo Trung - cuongnc.it659@gmail.com - Nguyễn Chí Cường.
    *
    */

	class model_news extends my_model
	{
		protected $_table;

		public function __construct()
        {
            parent::__construct();
            $this->_table = "tbl_news";
        }

		public function getNews()
		{
			$this->where(array('news_status'=>1,
								'news_hightlight'=>1,
			));
			$this->order('news_created', 'DESC');
			$this->get($this->_table);

			if($this->num_rows() > 0)
				return $this->fetch_all();
			return FALSE;
		}

        public function getAllNewsBySlug($data = array())
        {
            if(!empty($data))
            {
                $this->where($data);
                $this->get($this->_table);

                if($this->num_rows() > 0)
                    return $this->fetch_all();
                return FALSE;
            }
            return FALSE;
        }

        public function getNewsDetail($slug)
		{
			$this->where(array('slug'=>$slug));
			$this->get($this->_table);

			if($this->num_rows() > 0)
				return $this->fetch_one();
			else
				return FALSE;
		}
		public function getNewLike($data = array())
		{
			$titles = $data['news_title'];
			$title_array = explode(" ", $titles);

			/*echo "<pre>";print_r($data);*/
			if(count($title_array) == 1)
            {
            	$strSQL = "SELECT * FROM $this->_table WHERE news_title LIKE '%$title_array[0]%' AND news_id <> ".$data['news_id']." ORDER BY news_id DESC LIMIT 0,4";

          		$this->query($strSQL);
            }
            else
            {
                $strSQL = "SELECT * FROM $this->_table WHERE ( ";

                $count = 0;

                foreach($title_array as $title):
                    if($count == 0):
                        $strSQL .= " news_title LIKE '%$title%'";
                    else:
                        $strSQL .= " OR news_title LIKE '%$title%'";
                    endif;
                    $count ++;
                endforeach;

				$strSQL .= " ) AND  news_id <> ".$data['news_id']." ORDER BY news_id DESC LIMIT 0,4";

				//print($strSQL);

               	$this->query($strSQL);
            }

			if($this->num_rows() > 0)
				return $this->fetch_all();
			return FALSE;
		}

		public function updateCounter($data = array())
		{
			if(!empty($data) && isset($data['news_id']))
			{
				$strSQL = "UPDATE ".$this->_table." SET news_counter = news_counter + 1 WHERE news_id = ".$data['news_id'];

			}
			else
			{
				$strSQL = "UPDATE ".$this->_table." SET news_counter = news_counter + 1";
			}

			$this->query($strSQL);
		}


	}

    /*End of layout.php*/
    /*Location: my_framework/controllers/layout.php */